package com.cwz.gulimall.authserver.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.cwz.gulimall.authserver.feign.MemberFeignService;
import com.cwz.gulimall.common.constant.AuthServerConstant;
import com.cwz.gulimall.common.vo.MemberRespVo;
import com.cwz.gulimall.authserver.vo.SocialUser;
import com.cwz.gulimall.authserver.vo.User;
import com.cwz.gulimall.common.utils.HttpUtils;
import com.cwz.gulimall.common.utils.R;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.HashMap;

/*
    处理社交登录
 */
@Controller
public class OAuth2Controller {

    @Autowired
    MemberFeignService memberFeignService;

    @GetMapping("/oauth2.0/gitee/success")
    public String gitee(@RequestParam("code") String code, HttpSession session) throws Exception {
        HashMap<String, String> map = new HashMap<>();
        map.put("grant_type", "authorization_code");
        map.put("code", code);
        map.put("client_id", "d19c94c9da2af7d5bd753c5492472acf6b13308470adbe4a0386a16f498d3f0f");
        map.put("redirect_uri", "http://auth.gulimall.com/oauth2.0/gitee/success");
        map.put("client_secret", "c643b127b61fe9eb19c441a0b701d3731c81ed1ff8b33cd48a2137a260a92b20");
        HttpResponse response = HttpUtils.doPost("https://gitee.com", "/oauth/token", "post", new HashMap<>(), null, map);
        if (response.getStatusLine().getStatusCode() == 200) {
            String json = EntityUtils.toString(response.getEntity());
            SocialUser socialUser = JSON.parseObject(json, SocialUser.class);
            HashMap<String, String> map1 = new HashMap<>();
            map1.put("access_token", socialUser.getAccess_token());
            HttpResponse response1 = HttpUtils.doGet("https://gitee.com", "/api/v5/user", "get", new HashMap<>(), map1);
            if (response1.getStatusLine().getStatusCode() == 200) {
                String user = EntityUtils.toString(response1.getEntity());
                User user1 = JSON.parseObject(user, User.class);
                socialUser.setNickName(user1.getName());
                socialUser.setUid(user1.getId());
                R login = memberFeignService.oauthLogin(socialUser);
                if (login.getCode() == 0) {
                    MemberRespVo data = login.getData("data", new TypeReference<MemberRespVo>() {
                    });
                    session.setAttribute(AuthServerConstant.SESSION_LOGIN_KEY, data);
                    return "redirect:http://gulimall.com";
                } else {
                    return "redirect:http://auth.gulimall.com/login.html";
                }
            } else {
                return "redirect:http://auth.gulimall.com/login.html";
            }
        } else {
            return "redirect:http://auth.gulimall.com/login.html";
        }
    }
}
